$(document).ready(function(e){
	var allpath=$("#path").val();		
	var fromnum=3;
$("#addform").click(function(){
		if(fromnum==7){
		return false;
		}
		var newf=fromnum+1;
$("#fclass"+fromnum).after("<form id='fclass"+newf+
"'><span>分类名称&nbsp;</span><input type='text' name='name'><span>&nbsp;价格&nbsp;</span>"
+"<input type='text' name='price'><span>&nbsp;库存&nbsp;</span><input type='text' name='stock'>"+
" <input type='hidden' name='commid' class='commid'></form>");
		fromnum=newf;
});
$("#yesClass").click(function(){
		$("#addClassDiv").css("display","none");
});
$("#notClass").click(function(){
		$("#addClassDiv").css("display","none");
	/* 	取消同时清除val值 
		var arr=$("#addClassDiv input");
			for(var i=0;i<arr.length;i++){
				$(arr[i]).val("");
			} */
});
$("#addClass").click(function(){
		$("#addClassDiv").css("display","block");
		
});
$("#submits").click(function(){
	
});
	$("#upload").change(function(){
		var file = $("#upload").val();
			if (!/.(gif|jpg|jpeg|png|GIF|JPG|png)$/.test(file)) {
				alert("图片类型必须是.gif,jpeg,jpg,png中的一种");
				return false;
			}
			/* 创建表单对象 */
			var formData = new FormData();
				/* 添加文件和id */
			formData.append("img", $("#upload")[0].files[0]);
			$.ajax({
				url :allpath+"commodity/uploadImg.action",
				type : 'POST',
				data : formData,
				processData : false,
				contentType : false,
				dataType : "json",
				success : function(data) {
						$("#newimg").val(data.img);
						$("#notimg").attr("src","file/img/"+data.img);	
				},
				error : function(responseStr) { 
					console.log("error");
					} 
				});
	});
	$("#submits").click(function(){
		var title=$("#title").val();
		var price=$("#price").val();
		var brand=$("brand").val();
		var parameter=$("#parameter").val();
		var stock=$("#stock").val();
		var newimg=$("#newimg").val();
		var test=/[0-9]{1,20}/;
		if(title==''||price==''||brand==''||parameter==''||stock==''||newimg==''){
				$("#error").html("所有项目不能为空,请填写!");
				return false;
		}
		if(!test.test(price)){
			$("#error").html("价格只能为数字!");
			return false;
		}
		if(!test.test(stock)){
			$("#error").html("库存只能为数字!");
				return false;
		}
		/*用ajax提交商品,得到商品id再添加商品分类信息*/
				var commids;
		var form=$("#commodityform").serializeArray();
		$.post(allpath+"commodity/addCommodity.action",form,function(data){
				commids=data.commid;
				$(".commid").each(function(){
					$(this).val(commids);
				});
				for(var i=0;i<fromnum;i++){
				var b=i+1;
				var val=$("#fclass"+b).serializeArray();
				if(val[0].value!=null&&val[0].value!=''){
		$.post(allpath+"CommClassController/addClass.action",val,function(data){
			if(data.result==0){
			}});}
				}
				if(confirm("商品添加成功是否继续填写详细描述页")){
					window.open(allpath+"CommDetailedController/getDetailed.action?commid="+commids);
		}else{
				location.reload();	
		}
			});
			return false;
	});
	/* 滚动图添加 */
	$("#uploadImgs").click(function(){
			$("#addImg").css("display","block");
			return false;
	});
	
		var img0,img1,img2,img3;
		var imgpath;
		var list;
	$("#uploads").change(function(e) {
		var imgs=document.getElementById("uploads").files;
		if(imgs.length>4){
			alert("一次不能上传超过4张图片");
			return false;
		}
		// 创建表单对象 
		var formData = new FormData();
			for(var i=0;i<imgs.length;i++){
//				 获取文件名,判断是否是图片 
				if (!/.(gif|jpg|jpeg|png|GIF|JPG|png)$/.test(imgs[i].name)) {
					alert("图片类型必须是.gif,jpeg,jpg,png中的一种");
					return false;
				}
				 //添加文件和id 
				formData.append("img"+i, imgs[i]);
			}
			var urlpath=$("#urlfast").val();
		$.ajax({
			url :allpath+"commodity/uploadImgsTo.action",
			type : 'POST',
			data : formData,
			processData : false,
			contentType : false,
			dataType : "json",
			success : function(data) {
				img0=data.img0;
				img1=data.img1;
				img2=data.img2;
				img3=data.img3;
				list=[img0,img1,img2,img3];
					if(list[0]!=null){
						$("#img0").attr("src","file/detailed/"+list[0]);
						$("#closImg0").css("display","inline");
					}
					if(list[1]!=null){
						$("#img1").attr("src","file/detailed/"+list[1]);	
						$("#closImg1").css("display","inline");
					}
					if(list[2]!=null){
						$("#img2").attr("src","file/detailed/"+list[2]);	
						$("#closImg2").css("display","inline");
					}
					if(list[3]!=null){
						$("#img3").attr("src","file/detailed/"+list[3]);	
						$("#closImg3").css("display","inline");
					}
				$("#submitImg").css("display","inline");
				$("#files").css("display","none");
				$("#imgspan").css("display","none");
					
			},
			error : function(responseStr) { 
				console.log("error");
				} 
			});
});
	/* 确认插入图片按钮*/
	$("#submitImg").click(function(){
		if(list[0]!=null){
			
			$("#formImg1").val(list[0]);
		}
		if(list[1]!=null){
			$("#formImg2").val(list[1]);
					}
		if(list[2]!=null){
			$("#formImg3").val(list[2]);
					}
		if(list[3]!=null){
			$("#formImg4").val(list[3]);
					}
		$("#files").css("display","inline-block");
		$("#addImg").css("display","none");
		$("#submitImg").css("display","none");
	});	
//删除将要插入的图片
$("#closImg0").click(function(){
	var src=list[0];
	var val={'imgName':src};
	var url=allpath+"commodity/deleteImg.action"; 
	$.post(url,val,function(data){
		
	});
	$("#img0").attr("src","img/imgDefault.png");
	$("#closImg0").css("display","none");
	list[0]=null;
	
});
$("#closImg1").click(
		function(){
			var src=list[1];
			var val={'imgName':src};
			var url=allpath+"commodity/deleteImg.action"; 
			$.post(url,val,function(data){
				
			});
			$("#img1").attr("src","img/imgDefault.png");
			$("#closImg1").css("display","none");
			list[1]=null;
		}
);
$("#closImg2").click(function (){
	var src=list[2];
	var val={'imgName':src};
	var url=allpath+"commodity/deleteImg.action"; 
	$.post(url,val,function(data){
		
	});
	$("#img2").attr("src","img/imgDefault.png");
	$("#closImg2").css("display","none");
	list[2]=null;
});
$("#closImg3").click(function(){
	var src=list[3];
	var val={'imgName':src};
	var url=allpath+"commodity/deleteImg.action"; 
	$.post(url,val,function(data){
		
	});
	$("#img3").attr("src","img/imgDefault.png");
	$("#closImg3").css("display","none");
	list[3]=null;
});
$("#closImg").click(function() {
	$("#addImg").css("display", "none");
	for(var i=0;i<4;i++){
		if(list[i]!=null){
			var src=list[i];
			var val={'imgName':src};
			var url=allpath+"commodity/deleteImg.action"; 
			$.post(url,val,function(data){
				
			});
			$("#img"+i).attr("src","img/imgDefault.png");
			$("#closImg"+i).css("display","none");
			list[i]=null;
		}
	}
	$("#submitImg").css("display","none");
});
/*设置官方标配*/
$("#price").bind('input propertychange',function(e){
		$("#hprice").val($(this).val());
		return false;
});
$("#stock").bind('input propertychange',function(e){
	$("#hstock").val($(this).val());
	return false;
});
});